package com.example.lastwork.controller;


import com.example.lastwork.common.controller.LogicController;
import com.example.lastwork.dao.ShopDao;
import com.example.lastwork.dto.OrdersDTO;
import com.example.lastwork.dto.RestDTO;
import com.example.lastwork.entity.Orders;
import com.example.lastwork.entity.Shop;
import com.example.lastwork.entity.ShopItem;
import com.example.lastwork.service.ShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "门店服务接口")
@RestController
@RequestMapping("/ShopController")
public class ShopController extends LogicController<ShopService, ShopDao, Shop, Long> {
    /** DTO转化器 */
    @Resource
    private ModelMapper modelMapper;

    public ShopController(@Autowired ShopService service){
        super(service);
    }

    @ApiOperation("商品上架")
    @RequestMapping(value = "/onSale",method = RequestMethod.POST)
    public RestDTO<ShopItem> onSale(@ApiParam("商店商品ID") Long shopItemId){
        return RestDTO.success(getService().onSale(shopItemId));
    }

    @ApiOperation("商品下架")
    @RequestMapping(value = "/downSale",method = RequestMethod.POST)
    public RestDTO<ShopItem> downSale(@ApiParam("商店商品ID") Long shopItemId){
        return RestDTO.success(getService().downSale(shopItemId));
    }

    @ApiOperation("获取门店的订单信息")
    @RequestMapping(value = "getOrdersList",method = RequestMethod.POST)
    public RestDTO<List<OrdersDTO>> getOrdersList(@ApiParam("门店编号") Long shopId){
        List<Orders> ordersList = getService().getOrdersList(shopId);
        return RestDTO.success(ordersList.stream()
                .map(order -> modelMapper.map(order, OrdersDTO.class))
                .collect(Collectors.toList()));
    }

    @ApiOperation("获取所有的门店信息")
    @RequestMapping(value = "/getAllShop",method = RequestMethod.POST)
    public RestDTO<List<Shop>> getAllShop(){
        return RestDTO.success(getService().getAllShop());
    }


    @ApiOperation("根据商家编号获取门店信息")
    @RequestMapping(value = "/getMyShop",method = RequestMethod.POST)
    public RestDTO<List<Shop>> getMyShop(@ApiParam("商家编号") Long businessId){
        return RestDTO.success(getService().getMyShop(businessId));
    }
}
